<?php
header("Content-type:text/html;charset=utf-8");
$get_datas = file_get_contents('php://input');
if ($get_datas) {
    include '../../api/config.php';
    include './comm.php';
    //数据库配置文件
    $data = json_decode($get_datas);
    $id = $data->head;

    if ($id == '1051') {
        $ids = $data->id;
        $flg = $data->flg;
        $sql3 = "DELETE FROM subscribe WHERE id = '{$ids}'";
        if (flag($data)) {
            $stmt3 = $conn->prepare($sql3);
            $stmt3->execute();
            $result3 = $stmt3->setFetchMode(PDO::FETCH_ASSOC);
            if ($result3) {
                echo get_msg('200','操作成功~',false,false);
            } else {
                echo get_msg('400','数据异常~',false,false);
            }
        } else {
            echo get_msg('500','权限不足~','../index.html',false);
        }
    }

    // 搜索
    if ($id == '1062') {
        $key = $data->keys;
        $flg = $data->flg;
        $page = $data->page;
        $page = ($page - 1) * 10;
        $sql2 = "SELECT count(*) FROM subscribe WHERE flg='{$flg}'";
        $sql9 = "SELECT * FROM subscribe WHERE (email LIKE '%{$key}%' OR id LIKE '%{$key}%') AND flg='{$flg}';";
        if (flag($data)) {
            $arr1 = array();
            $arr1['data'] = array();
            $stmt1 = $conn->query($sql9);
            $stmt1->execute();
            $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
            foreach (new RecursiveArrayIterator($stmt1->fetchAll()) as $k1 => $v1) {
                array_push($arr1['data'], $v1);
                //添加到数组
            }
            $stmt2 = $conn->query($sql2);
            $stmt2->execute();
            $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
            $all_page = $stmt2->fetchAll()[0]["count(*)"];
            if ($result1) {
                $arr1['code'] = '200';
                $arr1['all_page'] = $all_page / 10;
                $arr1['msg'] = '获取数据成功~';
                $data = json_encode($arr1);
                //把数据转换为JSON数据.
                echo $data;
            } else {
                echo get_msg('400','数据异常~',false,false);
            }
        } else {
            echo get_msg('500','权限不足~','../index.html',false);
        }
    }

    if ($id == '1050') {
        $page = $data->page;
        $page = ($page - 1) * 10;
        $sql1 = "SELECT * FROM subscribe WHERE flg=1 ORDER BY id DESC LIMIT {$page},10";
        $sql2 = "SELECT count(*) FROM subscribe WHERE flg=1";
        if (flag($data)) {
            $arr1 = array();
            $arr1['data'] = array();
            $stmt1 = $conn->query($sql1);
            $stmt1->execute();
            $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
            foreach (new RecursiveArrayIterator($stmt1->fetchAll()) as $k1 => $v1) {
                array_push($arr1['data'], $v1);
                //添加到数组
            }
            $stmt2 = $conn->query($sql2);
            $stmt2->execute();
            $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
            $all_page = $stmt2->fetchAll()[0]["count(*)"];
            if ($result1) {
                $arr1['code'] = '200';
                $arr1['all_page'] = $all_page / 10;
                $arr1['msg'] = '获取数据成功~';
                $data = json_encode($arr1);
                //把数据转换为JSON数据.
                echo $data;
            } else {
                echo get_msg('400','数据异常~',false,false);
            }
        } else {
            echo get_msg('500','权限不足~','../index.html',false);
        }
    }
    // 输出内容

    if ($id == 20001) {
        $sql1 = "SELECT * FROM subscribe WHERE flg=1";
        if (flag($data)) {
            $arr1 = array();
            $arr1['data'] = array();
            $stmt1 = $conn->query($sql1);
            $stmt1->execute();
            $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
            $e_data = $stmt1->fetchAll();
            $email_lens = count($e_data);
            $email_data = "";
            for($i = 0; $i < $email_lens;$i++){
                if($i === ($email_lens-1)){
                    $email_data.=$e_data[$i]['email'];
                }else{
                    $email_data.=$e_data[$i]['email'].';';
                };
            };
            if ($result1) {
                echo get_msg('200','导入成功~',false,$email_data);
            } else {
                echo get_msg('400','数据异常~',false,false);
            }
        } else {
            echo get_msg('500','权限不足~','../index.html',false);
        }
    }//群发邮件

    $conn = null;
} else {
    echo $get_datas;
    echo "<script>\r\n    window.location.href = '../../';\r\n    </script>";
}